package controllers
{
	import com.greensock.TweenLite;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;

	import mx.core.UIComponent;

	import views.anime.AnimeCD;
	import views.anime.AnimeData;
	import views.anime.AnimePlayer;
	import views.components.LoadingPopup;
	import views.components.MCCreator;
	import views.components.NameGenerator;
	import views.components.UploaderPopup;
	import views.editor.AlbumPublishView;
	import views.editor.PaintBGGenerator;

	public class DBPopUp
	{
		public function DBPopUp()
		{
		}

		public static var root:UIComponent;

		private static var loading:LoadingPopup;

		public static function addPopUp(window:DisplayObject):void
		{
			root.visible=true;
			root.mouseEnabled=root.mouseChildren=true;
			var dx:Number=root.width - window.width >> 1;
			var dy:Number=root.height - window.height >> 1;

			window.x=dx;
			window.y=dy;
			root.addChild(window);
		}

		public static function addCD(cb:Function):void
		{
			var cd:AnimeCD = new AnimeCD();
			addPopUp(cd);
			cd.init(cb,3);
			cd.addEventListener(Event.COMPLETE,onComplete);

			function onComplete(e:Event):void
			{
				cd.removeEventListener(Event.COMPLETE,onComplete);
				removePopUp(cd);
			}
		}

		public static function addPopUpEff(window:DisplayObject):void
		{
			root.visible=true;
			root.mouseEnabled=root.mouseChildren=true;
			var dx:Number=1024 - window.width >> 1;
			var dy:Number=768 - window.height >> 1;
			//			var dx:Number=2048 - window.width >> 1;
			//			var dy:Number=1536 - window.height >> 1;
			window.x=dx;
			window.y=dy;
			root.addChild(window);

			window.alpha=0;
			window.y=768;
			TweenLite.to(window,.5,{y:dy,alpha:1});
		}

		public static function removePopUp(window:DisplayObject,hideLayer:Boolean=true):void
		{
			root.visible=!hideLayer;
			if(root.contains(window))
				root.removeChild(window);
		}

		public static function addLoadingPopup(text:String, cb:Function):void
		{
			if (loading)
				removeLoadingPopup(false);
			loading=new LoadingPopup();
			loading.text=text;
			loading.cb=cb;
			addPopUp(loading);
		}

		public static function removeLoadingPopup(dispose:Boolean=true):void
		{
			if (loading)
			{
				removePopUp(loading);
				if (dispose)
					loading.dispose();
			}
			loading=null;
		}

		public static function addAnimeReplay(loader:PaintLoader,_data:AnimeData=null,_bg:BitmapData=null,_wavData:ByteArray=null):void
		{
			var player:AnimePlayer=new AnimePlayer();
			if(loader)
				player.initOnlineData(loader);
			else
				player.initCacheData(_data,_bg,_wavData);
			player.play();
			addPopUp(player);
			player.addEventListener('close',onClose);

			function onClose():void
			{
				player.removeEventListener('close',onClose);
				removePopUp(player);
			}
		}

		public static function addUpload(arr:Array, cb:Function):void
		{
			var uploader:UploaderPopup=new UploaderPopup();
			uploader.addEventListener('close',onClose);
			addPopUp(uploader);

			TweenLite.delayedCall(.2,uploader.initData,[arr,cb]);

			function onClose():void
			{
				uploader.removeEventListener('close',onClose);
				removePopUp(uploader);
				uploader=null;
			}
		}

		public static function addNameGenerator(defaultTxt:String,cb:Function):void
		{
			var nameG:NameGenerator = new NameGenerator();
			addPopUp(nameG);
			nameG.addEventListener(Event.COMPLETE,onComplete);
//            root.addEventListener(MouseEvent.CLICK,onCancel);
			if(defaultTxt)
				nameG.nameTxt.text = defaultTxt;
			function onComplete(e:Event):void
			{
				nameG.removeEventListener(Event.COMPLETE,onComplete);
//                root.removeEventListener(MouseEvent.CLICK,onCancel);
				removePopUp(nameG);

				if(cb!=null)
					cb(nameG.getName());
				cb=null;
				nameG=null;
			}

//            function onCancel(e:Event):void
//            {
//                nameG.removeEventListener(Event.COMPLETE,onComplete);
//                root.removeEventListener(MouseEvent.CLICK,onCancel);
//                removePopUp(nameG);
//                nameG=null;
//            }
		}

		public static function addPublishView(cb:Function):void
		{
			var publisher:AlbumPublishView = new AlbumPublishView();
			addPopUp(publisher);
			publisher.addEventListener(Event.COMPLETE,onComp);
			publisher.addEventListener(Event.CANCEL,onCancel);

			function onComp(e:Event):void
			{
				publisher.removeEventListener(Event.COMPLETE,onComp);
				publisher.removeEventListener(Event.CANCEL,onCancel);
				removePopUp(publisher);
				var obj:Object = publisher.getData();
				if(cb!=null)
					cb(obj);
				cb=null;
				publisher=null;
			}

			function onCancel(e:Event):void
			{
				publisher.removeEventListener(Event.COMPLETE,onComp);
				publisher.removeEventListener(Event.CANCEL,onCancel);
				removePopUp(publisher);
				publisher=null;
			}
		}

		public static function addImageGenerator(callback:Function):void
		{
			var bgg:PaintBGGenerator = new PaintBGGenerator();
			DBPopUp.addPopUp(bgg);
			bgg.addEventListener(Event.COMPLETE,onBGComp);
			bgg.addEventListener(Event.CANCEL,onBGCancel);

			function onBGComp(e:Event):void{
				callback(bgg.getBG());

				bgg.removeEventListener(Event.COMPLETE,onBGComp);
				bgg.removeEventListener(Event.CANCEL,onBGCancel);
				DBPopUp.removePopUp(bgg);
				bgg=null;
			}

			function onBGCancel(e:Event = null):void{
				bgg.removeEventListener(Event.COMPLETE,onBGComp);
				bgg.removeEventListener(Event.CANCEL,onBGCancel);
				DBPopUp.removePopUp(bgg);
				bgg=null;
			}
		}


		public static function addImageUploader(callback:Function):void
		{
			var fref:FileReference=new FileReference();
			fref.browse([new FileFilter("请选择图片","*.jpg;*.png")]);
			fref.addEventListener(Event.SELECT,onSelected);
			function onSelected(event:Event):void
			{
				fref.load();
				fref.addEventListener(Event.COMPLETE,onComplete);
			}
			function onComplete(event:Event):void
			{
				var loader:Loader=new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
				loader.loadBytes(fref.data);
			}

			function onLoadComplete(e:Event):void
			{
				var li:LoaderInfo=e.target as LoaderInfo;
				var bd:Bitmap=li.content as Bitmap;

				if(callback!=null)
					callback(bd.bitmapData);
				callback=null;
			}
		}
	}
}


